package com.example.pungolo.journal.model;

import java.util.Calendar;
import java.util.Date;

import android.content.Intent;
import android.util.Log;

public class DiaryEntry {
  Mood mood;
  String text;
  Date date;

  /**
   * Creates a new instance of DiaryEntry with default values
   */
  public DiaryEntry() {
    mood = null;
    text = "";
    date = Calendar.getInstance().getTime();
  }

  public Mood getMood() {
    return mood;
  }

  public String getText() {
    return text;
  }

  public Date getDate() {
    return date;
  }

  public void setDate(Date date) {
    this.date = date;
  }

  public void setMood(Mood mood) {
    this.mood = mood;
  }

  public void setText(String text) {
    this.text = text;
  }

  public void save() {
    DiaryIO.getDatabase().saveDiaryEntry(this);
  }
  
  public boolean delete() {
    return DiaryIO.getDatabase().deleteDiaryEntry(this);
  }
  
  public Intent getSharingIntent() {
    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    String s = "";
    if (this.getText() != null)
      s = this.getText();

    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, s);

    Log.i("pungolo-journal", "Calling intent for sharing \"" + s + "\"");
    return Intent.createChooser(sharingIntent, "Share via");
  }

  @Override
  public int hashCode() {
    return date.hashCode();
  }
  
  @Override
  protected DiaryEntry clone() {
    DiaryEntry e = new DiaryEntry();
    e.date = this.date;
    e.mood = this.mood;
    e.text = this.text;
    return e;
  }
  
  @Override
  public boolean equals(Object o) {
    if (o == null || !(o instanceof DiaryEntry))
      return false;
    return this.date.equals(((DiaryEntry)o).date);
  }
}
